package com.heima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.health.dto.SetmealDto;
import com.heima.health.entity.PageResult;
import com.heima.health.entity.QueryPageBean;
import com.heima.health.mapper.SetmealMapper;
import com.heima.health.pojo.Setmeal;
import com.heima.health.service.CheckGroupService;
import com.heima.health.service.SetmealService;
import com.heima.health.utils.aliyunoss.AliyunUtils;
import com.heima.health.utils.redis.RedisUtil;
import com.heima.health.utils.resources.RedisConstant;
import com.heima.health.vo.CheckGroupVO;
import com.heima.health.vo.SetmealVO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private CheckGroupService checkGroupService;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page pageData=null;
        QueryWrapper<Setmeal> wrapper=new QueryWrapper<>();
        if (queryPageBean.getQueryString()==null){
            pageData=page(new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()));
        }else {
            wrapper.like("NAME",queryPageBean.getQueryString()).or().like("HELPCODE",queryPageBean.getQueryString());
            pageData=page(new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()),wrapper);
        }
        return new PageResult(pageData.getTotal(),pageData.getRecords());
    }

    @Override
    public Boolean saveUpdate(SetmealDto setmealDto) {
        Integer sid = setmealDto.getId();
        if (sid!=null){
            //更新操作，先删除中间表的sid对应的gid
            baseMapper.deleteGidBySid(sid);
            String imgInDb = baseMapper.findImgBySid(sid);
            //在更新图片地址之前，将之前图片地址加入redis中以保证更新完成后在oss中清楚图片
            if (!imgInDb.equals(setmealDto.getImg())){
                RedisUtil.addToSet(RedisConstant.ALL_SETMEAL_PIC_SET,imgInDb);
            }
        }
        boolean flag = saveOrUpdate(setmealDto);
        sid=setmealDto.getId();
        Integer[] groupIds = setmealDto.getGroupIds();
        if (groupIds!=null){
            for (Integer groupId : groupIds) {
                baseMapper.insertGidBySId(sid,groupId);
            }
        }

        if(setmealDto.getImg()!=null){
            //将图片id存入数据库后在redis中移除
            RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,setmealDto.getImg());
        }
        return flag;
    }

    @Override
    public void clearOssImg() {
        //获取redis所有集合数据比对时效redis数据，找不到时在oos删除
        Set<String> members = RedisUtil.getMembersOfSet(RedisConstant.ALL_SETMEAL_PIC_SET);
        if (members!=null&&members.size()!=0){
            for (String member : members) {
                if (!RedisUtil.existsKey(RedisConstant.SINGLE_PIC+member)) {
                    AliyunUtils.deleteFile(member);
                    RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,member);
                    System.out.println("=========图片清理完成==========");
                }
                
            }
        }
    }

    @Override
    public SetmealVO findBySid(Integer sid) {
        SetmealVO setmealVO=new SetmealVO();
        setmealVO.setCheckgroupIds(baseMapper.findIds(sid));
        setmealVO.setCheckGroups(checkGroupService.list());
        return setmealVO;
    }
}
